<?php
date_default_timezone_set('Asia/Shanghai');

$mysql_server_name='127.0.0.1'; //改成自己的mysql数据库服务器
 
$mysql_username='roots'; //改成自己的mysql数据库用户名
 
$mysql_password='620500s'; //改成自己的mysql数据库密码
 
$mysql_database='cdzx'; //改成自己的mysql数据库名

$conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password) or die("数据库链接错误") ; //连接数据库
 
mysql_query("set names 'utf8'"); //数据库输出编码 
 
mysql_select_db($mysql_database); //打开数据库
 
$sql ="select * from config "; //SQL语句
 
$result = mysql_query($sql,$conn); //查询
$row = mysql_fetch_array($result);
echo "数据库连接成功....".'<br />';
//权重
$w = $row['weight'];
//赠送积分比
$r = $row['return'];


//查询自定义返比开始
$year = date('Y', time());      //年
$moth = date('m', time());      //月
$day  = date('d', time());      //日

$custom_ratiosql        ="select * from integral where year = ".$year." and moth = ".$moth; //查询自定义返比
$custom_ratio_result    = mysql_query($custom_ratiosql,$conn); //查询
$custom_ratio_row       ;

while ($Rowss = mysql_fetch_assoc  ($custom_ratio_result)) {
    $custom_ratio_row = $Rowss;
}

if($custom_ratio_row && $custom_ratio_row['ratio'] && is_numeric($custom_ratio_row['ratio'])){
    $r = $custom_ratio_row['ratio'];
}
//查询自定义返比结束

$newShijian = date('d', time());

//获取所有用户信息
$userSql ="select * from user"; //SQL语句
$userResult = mysql_query($userSql,$conn); //查询
echo "开始查找所有用户....".'<br />';
echo "开始计算积分单元,需要大约5分钟....".'<br />';
while($userRow = mysql_fetch_array($userResult))
{	
		
	
	/*Start 开始前计算*/
	/*计算每个用户的消费积分,总数*/
	$my_xf_sql = "select * from log where user_Id = ".$userRow['user_Id']." and reason = '消费积分'";
	$my_xf = mysql_query($my_xf_sql,$conn);
	$my_xf_points = 0;
	while($my_xf_row = mysql_fetch_array($my_xf))
	{
		$my_xf_points = $my_xf_row['points'] + $my_xf_points;
	}
	

	/*计算累计积分单元    总消费积分数/权重（如权重=500）  =  积分单元  */   

	$my_cSql ="select * from config where Id = 1"; //SQL语句
	$my_cResult = mysql_query($my_cSql,$conn);
	$my_cRow = mysql_fetch_array($my_cResult);

    $my_weight = $my_cRow['weight'];                //取得权重
	$my_unit = floor($my_xf_points/$my_weight);
	if($my_unit < 0)
	{
		$my_unit = 0;
	}

	/*已经返的积分*/
	$my_wSql ="select * from weight where user_id = ".$userRow['user_Id']." and is_user = 1"; //SQL语句
	$my_wResult = mysql_query($my_wSql,$conn); //查询
	
	$my_back = 0;
	while($my_wRow = mysql_fetch_array($my_wResult))
	{
		$my_back = $my_wRow['back'] + $my_back;
	}
	$my_back_unit = floor($my_back/$my_weight);
	if($my_back_unit < 0)
	{
		$my_back_unit = 0;
	}	
	$my_isunit = $my_unit - $my_back_unit;

	/*更新数据*/
	$my_uSql ="update user set unit= ".$my_unit." , isunit = ".$my_isunit." where user_Id = ".$userRow['user_Id']; //SQL语句
	$my_uResult = mysql_query($my_uSql,$conn);	
	
	/*End 开始前计算*/	
	
	
	
	//可以返的积分单元--用户weight表
	//$isUnit = $userRow['isunit'];
	$isUnit = $my_isunit;
	
	if($isUnit)
	{	
		if($newShijian == '01' || $newShijian == '16')
		{
			//一个积分单元返还的金额
			$retuen_money = $w*$r;
			$allRetuen_money = $retuen_money*$isUnit;       //所有积分单元，可返现金

			$user_id = $userRow['user_Id'];
			$is_user = 1;
			$back    = $allRetuen_money;
			$shijian = date('Y-m-d h:i:s', time());

			$weightSql ="insert into weight(user_id, is_user, weight, ratio, back, shijian) 
			                   values('$user_id', '$is_user', '$w', '$r', '$back', '$shijian');"; //SQL语句
	 
			$weightRes = mysql_query($weightSql,$conn); //执行

		}
	}
	

	
	/*计算可以反的积分单元*/
	$wSql ="select * from weight where user_id = ".$userRow['user_Id']." and is_user = 1"; //SQL语句
	$wResult = mysql_query($wSql,$conn); //查询
	
	$back = 0;
	while($wRow = mysql_fetch_array($wResult))
	{
		$back = $wRow['back'] + $back;
	}
	
	$cSql ="select * from config where Id = 1"; //SQL语句
	$cResult = mysql_query($cSql,$conn);
	$cRow = mysql_fetch_array($cResult);

    $weight = $cRow['weight'];
	$unit = floor($back/$weight);
	if($unit < 0)
	{
		$unit = 0;
	}
	$isunit = $userRow['unit'] - $unit;
	$uSql ="update user set isunit = ".$isunit." where user_Id = ".$userRow['user_Id']; //SQL语句
	$uResult = mysql_query($uSql,$conn);
	/*计算可以反的积分单元end*/	
    file_put_contents('a.txt',$userRow['user_Id']);
}
echo "数据写入完成!".'<br />';

file_put_contents('b.txt',$newShijian.date('Y-m-d H:i:s',time()));
